package com.group.mall.service.Impl;

import com.group.mall.domain.POJO.Evaluation;
import com.group.mall.domain.POJO.Goods;
import com.group.mall.mapper.EvaluationMapper;
import com.group.mall.mapper.GoodsMapper;
import com.group.mall.service.EvaluationService;
import com.group.mall.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class EvaluationServiceImpl implements EvaluationService {

    @Autowired
    private EvaluationMapper evaluationMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private OrdersService ordersService;

    @Override
    @Transactional
    public Boolean addEva(Evaluation evaluation) {

        //修改订单状态：已评价，订单结束
        //todo:未测试
        Boolean result = ordersService.updateOrderStatus(evaluation.getOrderId(), 6);
        //修改商品评分
        if (result) {
            Goods goods = goodsMapper.selectGoodsById(evaluation.getGoodsId());
            float score = (goods.getScore() * goods.getSold() + evaluation.getScore())/(goods.getSold() + 1);
            result = goodsMapper.updateGoodsScore(evaluation.getGoodsId(), score);

            if (result){
                result = evaluationMapper.insertEva(evaluation);
            }
        }
        //插入评价记录
        return result;
    }
}
